package ${daoClassPath};

import ${entityClassAllName};
import fan.codeGenerate.analysisXML.AnalysisXML;
import fan.codeGenerate.databse.ConnectOracle;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/** 
* @author liu_fan 2017年3月31日 上午8:21:22  
*/
public class ${daoClassName} {	
	private Map<String, Object> jdbcInfo = null;
	public void connectOracle(){
		AnalysisXML ana = new AnalysisXML("src/main/java/fan/codeGenerate/generateConfigure.xml");
    	jdbcInfo = ana.getJdbcInfo();
	}
	public void saveOrUpdate(String sql){
		this.connectOracle();
		ConnectOracle conOra = new ConnectOracle(jdbcInfo);
		conOra.executeSaveOrUpdate(sql);
    	conOra.close();
	}
	public List<Map<String,Object>> select(String sql){
		List<Map<String,Object>> list = new ArrayList<Map<String,Object>>();
		this.connectOracle();
		ConnectOracle conOra = new ConnectOracle(jdbcInfo);
		list = conOra.executeSelect(sql);
    	conOra.close();
    	return list;
	}
	/**
	 * 根据entity内id进行删除
	 */
	public void delete(String id){
		StringBuilder sb = new StringBuilder();
		sb.append("delete ${tableName}");
		sb.append(" where id = '").append(id).append("'");
		this.saveOrUpdate(sb.toString());
	}
	/**
	 * 保存entity内的信息
	 */
	public void save(${entityClassName} entity){
		StringBuilder sb = new StringBuilder();
		sb.append(" insert into ${tableName}(");
		<#list columns as column>
		sb.append(" ${column.col},");
		</#list>
		sb.append(" )values(");
		<#list columns as value>
		<#if value.type == "VARCHAR2">
		sb.append(" '").append(entity.get${value.col}()).append("', ");
		</#if>
		<#if value.type == "DATE">
		sb.append(" to_date('").append(entity.get${value.col}()).append("','yyyy-MM-dd hh24:mi:ss'), ");
		</#if>
		<#if value.type == "NUMBER">
		sb.append(entity.get${value.col}()).append(", ");
		</#if>
		</#list>
		sb.append(" );");
		this.saveOrUpdate(sb.toString());
	}
	/**
	 * 根据entity内的编号更新以前的数据
	 */
	public void update(${entityClassName} entity){
		StringBuilder sb = new StringBuilder();
		sb.append(" update ${tableName} ");
		sb.append(" set ");
		<#list columns as column>
		if(entity.get${column.col}() != null){
			<#if column.type == "NUMBER">
			sb.append(" ${column.col}=").append(entity.get${column.col}()).append(",");
			</#if>
			<#if column.type == "VARCHAR2">
			sb.append(" ${column.col}='").append(entity.get${column.col}()).append("',");
			</#if>
			<#if column.type == "DATE">
			sb.append(" ${column.col}=to_date('").append(entity.get${column.col}()).append("','yyyy-MM-dd hh24:mi:ss'),");
			</#if>
		}
		</#list>
 		sb.append(" where id = ").append(entity.getId());
		this.saveOrUpdate(sb.toString());
	}
	
	public List<Map<String,Object>> getEntityById(String id){
		String sql = "select * from ${tableName} where id = "+id;
		return (List<Map<String, Object>>)this.select(sql).get(0);
	}
	
	public List<Map<String,Object>> getListByEntity(${entityClassName} entity){
		StringBuilder sb = new StringBuilder();
		sb.append(" Select * from ${tableName} where ");
		<#list columns as column>
		if(entity.get${column.col}() != null){
			<#if column.type == "NUMBER">
			sb.append(" ${column.col}=").append(entity.get${column.col}()).append(",");
			</#if>
			<#if column.type == "VARCHAR2">
			sb.append(" ${column.col}='").append(entity.get${column.col}()).append("',");
			</#if>
			<#if column.type == "DATE">
			sb.append(" ${column.col}=to_date('").append(entity.get${column.col}()).append("','yyyy-MM-dd hh24:mi:ss'),");
			</#if>
		}
		</#list>
		return this.select(sb.toString());
	}
}
